package com.lzx.resumeweb.api;

import com.lzx.resumebase.model.Result;
import com.lzx.resumebase.model.User;
import com.lzx.resumebase.util.ResultUtil;
import com.lzx.resumeservice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName UserApi
 * @Author 刘正星
 * @Date 2021/5/29 下午3:20
 * @Description
 */
@RestController
@RequestMapping("/user")
public class UserApi {
    @Autowired
    private UserService userService;
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable("id")String id){
       if (!StringUtils.hasText(id)){
           return ResultUtil.paramError();
       }
       return ResultUtil.success(userService.getById(id));
    }

    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody User user){
        if (null == user) {
            return ResultUtil.paramError();
        }
        //id为空插入
        if (!StringUtils.hasText(user.getId())){
            return ResultUtil.success(userService.save(user));
        }
        return ResultUtil.success(userService.updateById(user));
    }

    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id")String id){
        if (!StringUtils.hasText(id)){
            return ResultUtil.paramError();
        }
        return ResultUtil.success(userService.removeById(id));
    }
}
